home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_403_3.txt < prev    next >
Text File  |  1997-01-29  |  7KB  |  126 lines

  1. OPERATORS : operator precedence table, using masks
  2. _________________________________________________________________________
  3.  
  4.  
  5.                        OPERATOR PRECEDENCE TABLE
  6.     =================================================================
  7.    | ::        scope resolution          class_name::member          |
  8.    | ::        global                    ::name                      |
  9.    |-----------------------------------------------------------------|
  10.    | .         member selection          object.member               |
  11.    | ->        member selection          pointer->member             |
  12.    | []        subscripting              pointer[ expr ]             |
  13.    | ()        function call             expr( expr_list )           |
  14.    | ++        post increment            lvalue++                    |
  15.    | --        post decrement            lvalue--                    |
  16.    |-----------------------------------------------------------------|
  17.    | sizeof    size of object            sizeof expr                 |
  18.    | sizeof    size of type              sizeof( type )              |
  19.    | ++        pre increment             ++lvalue                    |
  20.    | --        pre decrement             --lvalue                    |
  21.    | ~         complement                ~expr (bitwise negation)    |
  22.    | !         not                       !expr                       |
  23.    | -         unary minus               -expr                       |
  24.    | +         unary plus                +expr                       |
  25.    | &         address of                &lvalue                     |
  26.    | *         dereference               *expr  (value at address)   |
  27.    | new       create (allocate)         new type                    |
  28.    | delete    destroy (de-allocate)     delete pointer              |
  29.    | delete[]  destroy array             delete[] pointer            |
  30.    | ()        cast (type conversion)    ( type )expr                |
  31.    |-----------------------------------------------------------------|
  32.    | .*        member selection          object.*pointer-to-member   |
  33.    | ->*       member selection          pointer->*pointer-to-member |
  34.    |-----------------------------------------------------------------|
  35.    | *         multiply                  expr * expr                 |
  36.    | /         divide                    expr / expr                 |
  37.    | %         modulo (remainder)        expr % expr                 |
  38.    |-----------------------------------------------------------------|
  39.    | +         add (plus)                expr + expr                 |
  40.    | -         subtract (minus)          expr - expr                 |
  41.    |-----------------------------------------------------------------|
  42.    | <<        shift left                expr << expr                |
  43.    | >>        shift right               expr >> expr                |
  44.    |-----------------------------------------------------------------|
  45.    | <         less than                 expr < expr                 |
  46.    | <=        less than or equal to     expr <= expr                |
  47.    | >         greater than              expr > expr                 |
  48.    | >=        greater than or equal to  expr >= expr                |
  49.    |-----------------------------------------------------------------|
  50.    | ==        equal to                  expr == expr                |
  51.    | !=        not equal to              expr != expr                |
  52.    |-----------------------------------------------------------------|
  53.    | &         bitwise AND               expr & expr                 |
  54.    |-----------------------------------------------------------------|
  55.    | ^         bitwise exclusive OR      expr ^ expr                 |
  56.    |                                     one or the other,           |
  57.    |                                     but not both                |
  58.    |-----------------------------------------------------------------|
  59.    | |         bitwise inclusive OR      expr | expr                 |
  60.    |-----------------------------------------------------------------|
  61.    | &&        logical AND               expr && expr                |
  62.    |-----------------------------------------------------------------|
  63.    | ||        logical inclusive OR      expr || expr                |
  64.    |-----------------------------------------------------------------|
  65.    | ? :       conditional expression    expr ? expr : expr          |
  66.    |-----------------------------------------------------------------|
  67.    | =         simple assignment         expr = expr                 |
  68.    | *=        multiply and assign       expr *= expr                |
  69.    | /=        divide and assign         expr /= expr                |
  70.    | %=        modulo and assign         expr %= expr                |
  71.    | +=        add and assign            expr += expr                |
  72.    | -=        subtract and assign       expr -= expr                |
  73.    | <<=       shift left and assign     expr <<= expr               |
  74.    | >>=       shift right and assign    expr >>= expr               |
  75.    | &=        AND and assign            expr &= expr                |
  76.    | |=        inclusive OR and assign   expr |= expr                |
  77.    | ^=        exclusive OR and assign   expr ^= expr                |
  78.    |-----------------------------------------------------------------|
  79.    | throw     throw exception           throw expr                  |
  80.    |-----------------------------------------------------------------|
  81.    | ,         comma (sequencing)        expr, expr                  |
  82.     ================================================================= 
  83.  
  84.  
  85.  
  86. USING MASKS
  87.  
  88. You can use the bitwise AND and OR operators to determine whether certain bits are 'on' or 'off', or to turn certain bits 'on' or 'off'.  Here's an example:
  89.  
  90.   // bits.cp
  91.   #include <iostream.h>
  92.   
  93.   #define kBit1 01
  94.   #define kBit2 02
  95.   #define kBit3 04
  96.   #define kBit4 010
  97.   #define kBit5 020
  98.   
  99.   void myCheckBits( short item );
  100.   
  101.   void main( void )
  102.   {
  103.     short flags = 0;
  104.   
  105.     // turn bits on
  106.     flags = flags | kBit2;
  107.     flags = flags | kBit4;
  108.     
  109.     myCheckBits( flags );
  110.   }
  111.   
  112.   void myCheckBits( short item )
  113.   {
  114.     // check if bits are on
  115.     if( item & kBit1 )
  116.       cout << "Bit 1 is on." << endl;
  117.     if( item & kBit2 )
  118.       cout << "Bit 2 is on." << endl;
  119.     if( item & kBit3 )
  120.       cout << "Bit 3 is on." << endl;  
  121.     if( item & kBit4 )
  122.       cout << "Bit 4 is on." << endl;  
  123.     if( item & kBit5 )
  124.       cout << "Bit 5 is on." << endl;  
  125.   }
  126.   // end bits.cp